<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * API login và trả về thông tin user đã đăng nhập
 *
 * @author notobo
 */

require APPPATH.'/libraries/REST_Controller.php';
//require APPPATH.'/models/modelUser.php';
session_start();
class User extends REST_Controller{
    
    public function __construct() {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->library('session');
    }
    
    function get_info_get(){
        $array = array();
        
        if(!$this->get('openid')){
            $this->response(array(
                'ret' => 1,
                'msg'=>'Missing parameter openid'
                ), 1);
        }else{
            $openid = $this->get('openid');           
        }
        
        if(!$this->get('openkey')){
            $this->response(array(
                'ret' => 1,
                'msg'=>'Missing parameter openkey'
                ), 1);
        }else{
           $openkey = $this->get('openkey');
           if(!$this->User_model->check_session_id($openid,$openkey)){
           //if($openkey !=  $this->session->userdata('my_session_id')){
               $this->response(array(
                   'ret' => 1,
                   'msg'=>'Parameter openkey is invalid'
                   ), 1);
           }
        }
        
        $data = $this->User_model->get_user($openid);
        if($data){
            foreach ($data as $row){
                $array = array(
                    'ret'=>0,
                    'is_lost'=>0,
                    'nickname'=>$row['nickname'],
                    'gender'=>$row['gender'],
                    'country'=>$row['country'],
                    'province'=>$row['province'],
                    'city'=>$row['city'],
                    'figureurl'=>$row['figureurl'],
                    'is_yellow_vip'=>$row['is_yellow_vip'],
                    'is_yellow_year_vip'=>$row['is_yellow_year_vip'],
                    'yellow_vip_level'=>$row['yellow_vip_level'],
                    'is_yellow_high_vip'=>$row['is_yellow_high_vip'],
                );
            } 
            $this->response($array,200);
        }else{
            $this->response(array(
                'ret' => 1,
                'msg'=>'Can not find user with openid = '.$openid,
                ), 1);
        }
    }
    public function is_login_get () {
    	
    	if ((!$this->get('openid')) || (!$this->get('openkey')) ) {
    		$this->response(array(
    				'ret' => 1,
    				'msg'=>'is not login',
    		), 1);
    	}
    	else {
    		$openid =  $this->get('openid');
    		$openkey = $this->get('openkey');
    	}
//     	if(($this->session->userdata('id') == $openid)&&($this->User_model->check_session_id(openid,$openkey))){
    	if($this->User_model->check_session_id($openid,$openkey)) {
    		 $this->response(array(
                'ret' => 0,
                'msg'=>'is login',
                ), 1);
    	}else{
    		 $this->response(array(
                'ret' => 1,
                'msg'=>'is not login',
                ), 1);
    	}
    }
    public function check_facebook_login(){
    	
    }
}

?>
